var page_size=10;
var p_cur = 1; //页号
var p_pages; //总页码
var p_totalRecords;
function queryConstant(){
    p_cur = 1;
    getConstant();
}
function getConstant(){
    var data = {
        "flag_name": $("#keywords").val()
    }
    getConstantList("/constant/getConstantByName/"+p_cur+"/"+page_size, data, function(data) {
        p_pages=data.pages;
        p_totalRecords=data.total;
        updateTable(data.list);
        pager();
    }, function(error) {
        alert(error);
    });
}

function updateTable(tableData){
    var cols = [
                {width: 80, text: '序号', type: 'number', flex: false, field: 'num'},
                {width: 80, text: '代码编号', type: 'date', flex: false, field: 'code_flag'},
                {width: 80, text: '代码名称', type: 'string', flex: true, field: 'code_name'},
                {width: 80, text: '有效标志', type: 'date', flex: false, field: 'valid'},
                {width: 80, text: '代码标识', type: 'string', flex: true, field: 'code_flag'},
                {width: 160, text: '标识名称', type: 'date', flex: false, field: 'flag_name'},
                {width: 80, text: '备注', type: 'string', flex: true, field: 'remark'},
                {width: 160, text: '操作', type: 'string', flex: true, field: 'id'}
            ];
    var rowData = formatTableData(tableData, cols);
    clearTable(cols);
    // 使用data参数更新数据：
    $('.datatable').datatable('load',  {
            cols: cols,
            rows:rowData
            // [
            //     {checked: false, data: [1, '1','上行','1','DIRECTION','方向','未稽查','<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']},
            //     {checked: false, data: [2, '2','下行','1','DIRECTION','方向','未稽查','<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']},
            //     // 更多数据
            // ]
        }
    );
}
function clearTable(cols){
     $('.datatable').datatable( {
        checkable:false,
        sortable:false,
        data: {
            cols: cols,
            rows:[]
        }
    });
}
function formatTableData(data,cols){
    var rows = [];
    // rows.push({checked: false, data: [1, '1','上行','1','DIRECTION','方向','未稽查','<a href="#" data-toggle="modal" data-target="#myModal"><i class="icon icon-edit"></i> 修改</a><a href="#"><i class="icon icon-remove-circle"></i> 删除</a>']});
    for(var i=0;i<data.length;i++){
        data[i].num=(p_cur-1)*10+i+1;
        rows.push(genRow(data[i],cols));
    }
    return rows;
}

function genRow(obj,cols){
    var row = {checked: false, data:null};
    var dArr=[];
    for(var i=0;i<cols.length;i++){
        if(cols[i].text=='操作') {
            var str='<a href="#" data-toggle="modal" onclick="editConstant('+obj[cols[i].field]+')"><i class="icon icon-edit"></i> 修改</a><a href="#" onclick="deleteConstant('+obj[cols[i].field]+')"><i class="icon icon-remove-circle"></i> 删除</a>';
            dArr.push(str);
        }else{
            dArr.push(obj[cols[i].field]);
        }
    }
    row.data=dArr;
    return row;
}
function pager(){
  
    kkpager.generPageHtml({
        pno : p_cur,
        total : p_pages,
        totalRecords : p_totalRecords,
        mode : 'click',
        click : function(n){
            p_cur = n;
            this.selectPage(p_cur);
            getConstant();//调用获取数据方法
            return false;
        }
    },true);
}   

function addConstant(){
    var valid=1;
    if(!$("#valid").is(':checked')) valid=0;
    var param ={
        'code_flag':$('#code_flag').val(),
        'flag_name':$('#flag_name').val(),
        'code_value':Number($('#code_value').val()),
        'code_name':$('#code_name').val(),
        'sort_no':Number($('#sort_no').val()),
        'remark':$('#remark').val(),
        'valid':valid
    };
    addConstant_service("/constant/add/", param, function(data) {
        alert(data);
        $('#addModal').modal('toggle');
    }, function(error) {
        alert(error);
    });
}

function editConstant(id){

}